package com.rwind.dynamic.controller;

import com.rwind.dynamic.report.JasperReportsUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("jasper")
public class MyJasperController {



    @GetMapping("/export")
    public void getReport(@RequestParam("type") String reportType, HttpServletResponse response)
            throws Exception {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("name", "xiao ming");

        List<HashMap> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("Field1",  "Field1-" + i);
            item.put("Field2",  "Field2-" + i);
            //图片可以填链接
            item.put("reportDescImage",  "https://i.328888.xyz/2023/04/24/iS185a.jpeg");
            //也可以填写本地图片文件路径
            //item.put("reportDescImage",  "D:\\BaiduNetdiskDownload\\my_huckleberry_friends.jpg");
            list.add(item);
        }
        String jasperPath = JasperReportsUtil.getJasperFileDir("demo");
        if (reportType.equals("pdf")) {
            JasperReportsUtil.exportToPdf(jasperPath, parameters, list, response);
        } else if (reportType.equals("html")) {
            JasperReportsUtil.exportToHtml(jasperPath, parameters, list, response);
        }
    }


}